package com.alibaba.fastjson.serializer;

import com.alibaba.fastjson.Student;
import com.alibaba.fastjson.util.ASMUtils;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * 依据Object类型，通过ASM字节码框架生成的JavaBean序列化器
 * 默认生成的6个方法
 * write
 * writeNormal
 * WriteDirectNonContext
 *
 * writeAsArray
 * writeAsArrayNormal
 * writeAsArrayNonContext
 */
public class ASMSerializer_1_Students extends JavaBeanSerializer implements ObjectSerializer {

    public Type age_asm_fieldType = ASMUtils.getMethodType(Student.class, "getAge");
    public ObjectSerializer age_asm_ser_;

    public ASMSerializer_1_Students(SerializeBeanInfo serializeBeanInfo) {
        super(serializeBeanInfo);
    }

    public void write(JSONSerializer jsonSerializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        if (object == null) {
            // 写null
            jsonSerializer.writeNull();
        } else {
            SerializeWriter serializeWriter = jsonSerializer.out;
            // 不直接写
            if (!this.writeDirect(jsonSerializer)) {
                this.writeNormal(jsonSerializer, object, fieldName, fieldType, features);
            } else if (serializeWriter.isEnabled(32768)) {
                // 不需要上下文直接写
                this.writeDirectNonContext(jsonSerializer, object, fieldName, fieldType, features);
            } else {
                Student student = (Student)object;
                // 上下文中不允许写ref
                if (!this.writeReference(jsonSerializer, object, features)) {
                    if (serializeWriter.isEnabled(2097152)) {
                        // 是数组
                        this.writeAsArray(jsonSerializer, object, fieldName, fieldType, features);
                    } else {
                        SerialContext serialContext = jsonSerializer.getContext();
                        jsonSerializer.setContext(serialContext, object, fieldName, 0);
                        char character = '{';
                        String ageStr = "age";
                        Integer age = student.getAge();
                        if (age == null) {
                            if (serializeWriter.isEnabled(260)) {
                                serializeWriter.write(character);
                                serializeWriter.writeFieldNameDirect(ageStr);
                                serializeWriter.writeNull(0, 256);
                                character = ',';
                            }
                        } else {
                            serializeWriter.write(character);
                            serializeWriter.writeFieldNameDirect(ageStr);
                            jsonSerializer.writeWithFieldName(age, ageStr, this.age_asm_fieldType, 0);
                            character = ',';
                        }

                        ageStr = "name";
                        String name = student.getName();
                        if (name == null) {
                            if (serializeWriter.isEnabled(132)) {
                                serializeWriter.write(character);
                                serializeWriter.writeFieldNameDirect(ageStr);
                                serializeWriter.writeNull(0, 128);
                                character = ',';
                            }
                        } else {
                            serializeWriter.writeFieldValueStringWithDoubleQuoteCheck(character, ageStr, name);
                            character = ',';
                        }

                        if (character == '{') {
                            serializeWriter.write(123);
                        }

                        serializeWriter.write(125);
                        jsonSerializer.setContext(serialContext);
                    }
                }
            }
        }
    }

    public void writeNormal(JSONSerializer var1, Object var2, Object var3, Type var4, int var5) throws IOException {
        if (var2 == null) {
            var1.writeNull();
        } else {
            SerializeWriter var9 = var1.out;
            Student var10 = (Student)var2;
            if (!var9.isEnabled(8192) && !var9.isEnabled(134217728)) {
                if (!this.writeReference(var1, var2, var5)) {
                    if (var9.isEnabled(2097152)) {
                        this.writeAsArrayNormal(var1, var2, var3, var4, var5);
                    } else {
                        SerialContext var11 = var1.getContext();
                        var1.setContext(var11, var2, var3, 0);
                        byte var10000;
                        if (var1.isWriteClassName(var4, var2) && var4 != var2.getClass()) {
                            var9.write(123);
                            this.writeClassName(var1, (String)null, var2);
                            var10000 = 44;
                        } else {
                            var10000 = 123;
                        }

                        char var12 = (char)var10000;
                        char var20 = this.writeBefore(var1, var2, var12);
                        boolean var13 = var9.isNotWriteDefaultValue();
                        var1.checkValue(this);
                        boolean var15 = var1.hasNameFilters(this);
                        String var6 = "age";
                        Object var8;
                        if (this.applyName(var1, var2, var6) && this.applyLabel(var1, "")) {
                            Integer var16 = var10.getAge();
                            if (var13) {
                            }

                            if (this.apply(var1, var2, var6, var16)) {
                                if (var15) {
                                    var6 = this.processKey(var1, var2, var6, var16);
                                }

                                var8 = this.processValue(var1, this.getBeanContext(0), var2, var6, var16);
                                if (var16 != var8) {
                                    if (var8 == null) {
                                        if (var9.isEnabled(260)) {
                                            var9.write(var20);
                                            var9.writeFieldName(var6, false);
                                            var9.writeNull(0, 256);
                                            var20 = ',';
                                        }
                                    } else {
                                        var9.write(var20);
                                        var9.writeFieldName(var6, false);
                                        var1.writeWithFieldName(var8, var6, this.age_asm_fieldType, 0);
                                        var20 = ',';
                                    }
                                } else if (var8 == null) {
                                    if (var9.isEnabled(260)) {
                                        var9.write(var20);
                                        var9.writeFieldName(var6, false);
                                        var9.writeNull(0, 256);
                                        var20 = ',';
                                    }
                                } else {
                                    var9.write(var20);
                                    var9.writeFieldName(var6, false);
                                    var1.writeWithFieldName(var8, var6, this.age_asm_fieldType, 0);
                                    var20 = ',';
                                }
                            }
                        }

                        var6 = "name";
                        if (this.applyName(var1, var2, var6) && this.applyLabel(var1, "")) {
                            String var17 = var10.getName();
                            if (var13) {
                            }

                            if (this.apply(var1, var2, var6, var17)) {
                                if (var15) {
                                    var6 = this.processKey(var1, var2, var6, var17);
                                }

                                var8 = this.processValue(var1, this.getBeanContext(1), var2, var6, var17);
                                if (var17 != var8) {
                                    if (var8 == null) {
                                        if (var9.isEnabled(132)) {
                                            var9.write(var20);
                                            var9.writeFieldName(var6, false);
                                            var9.writeNull(0, 128);
                                            var20 = ',';
                                        }
                                    } else {
                                        var9.write(var20);
                                        var9.writeFieldName(var6, false);
                                        var1.writeWithFieldName(var8, var6, String.class, 0);
                                        var20 = ',';
                                    }
                                } else if (var17 == null) {
                                    if (var9.isEnabled(132)) {
                                        var9.write(var20);
                                        var9.writeFieldName(var6, false);
                                        var9.writeNull(0, 128);
                                        var20 = ',';
                                    }
                                } else {
                                    var9.writeFieldValue(var20, var6, var17);
                                    var20 = ',';
                                }
                            }
                        }

                        var20 = this.writeAfter(var1, var2, var20);
                        if (var20 == '{') {
                            var9.write(123);
                        }

                        var9.write(125);
                        var1.setContext(var11);
                    }
                }
            } else {
                super.write(var1, var2, var3, var4, var5);
            }
        }
    }

    public void writeDirectNonContext(JSONSerializer var1, Object var2, Object var3, Type var4, int var5) throws IOException {
        if (var2 == null) {
            var1.writeNull();
        } else {
            SerializeWriter var9 = var1.out;
            Student var10 = (Student)var2;
            if (var9.isEnabled(2097152)) {
                this.writeAsArrayNonContext(var1, var2, var3, var4, var5);
            } else {
                char var11 = '{';
                String var6 = "age";
                Integer var12 = var10.getAge();
                if (var12 == null) {
                    if (var9.isEnabled(260)) {
                        var9.write(var11);
                        var9.writeFieldNameDirect(var6);
                        var9.writeNull(0, 256);
                        var11 = ',';
                    }
                } else {
                    var9.write(var11);
                    var9.writeFieldNameDirect(var6);
                    var1.writeWithFieldName(var12, var6, this.age_asm_fieldType, 0);
                    var11 = ',';
                }

                var6 = "name";
                String var13 = var10.getName();
                if (var13 == null) {
                    if (var9.isEnabled(132)) {
                        var9.write(var11);
                        var9.writeFieldNameDirect(var6);
                        var9.writeNull(0, 128);
                        var11 = ',';
                    }
                } else {
                    var9.writeFieldValueStringWithDoubleQuoteCheck(var11, var6, var13);
                    var11 = ',';
                }

                if (var11 == '{') {
                    var9.write(123);
                }

                var9.write(125);
            }
        }
    }

    public void writeAsArray(JSONSerializer var1, Object var2, Object var3, Type var4, int var5) throws IOException {
        SerializeWriter var9 = var1.out;
        Student var10 = (Student)var2;
        if (!var1.hasPropertyFilters(this)) {
            super.writeNoneASM(var1, var2, var3, var4, var5);
        } else {
            var9.write(91);
            String var6 = "age";
            Integer var11;
            if ((var11 = var10.getAge()) == null) {
                var9.writeNull();
            } else if (var11.getClass() == Integer.class) {
                if (this.age_asm_ser_ == null) {
                    this.age_asm_ser_ = var1.getObjectWriter(Integer.class);
                }

                ObjectSerializer var12 = this.age_asm_ser_;
                if (var12 instanceof JavaBeanSerializer) {
                    ((JavaBeanSerializer)var12).writeAsArrayNonContext(var1, var11, var6, Integer.class, 0);
                } else {
                    var12.write(var1, var11, var6, Integer.class, 0);
                }
            } else {
                var1.writeWithFieldName(var11, var6, this.age_asm_fieldType, 0);
            }

            var9.write(44);
            var6 = "name";
            var9.writeString(var10.getName(), ']');
        }
    }

    public void writeAsArrayNormal(JSONSerializer var1, Object var2, Object var3, Type var4, int var5) throws IOException {
        SerializeWriter var9 = var1.out;
        Student var10 = (Student)var2;
        if (!var1.hasPropertyFilters(this)) {
            super.writeNoneASM(var1, var2, var3, var4, var5);
        } else {
            var9.write(91);
            String var6 = "age";
            Integer var11;
            if ((var11 = var10.getAge()) == null) {
                var9.writeNull();
            } else if (var11.getClass() == Integer.class) {
                if (this.age_asm_ser_ == null) {
                    this.age_asm_ser_ = var1.getObjectWriter(Integer.class);
                }

                ObjectSerializer var12 = this.age_asm_ser_;
                var12.write(var1, var11, var6, Integer.class, 0);
            } else {
                var1.writeWithFieldName(var11, var6, this.age_asm_fieldType, 0);
            }

            var9.write(44);
            var6 = "name";
            var9.writeString(var10.getName(), ']');
        }
    }

    public void writeAsArrayNonContext(JSONSerializer var1, Object var2, Object var3, Type var4, int var5) throws IOException {
        SerializeWriter var9 = var1.out;
        Student var10 = (Student)var2;
        if (!var1.hasPropertyFilters(this)) {
            super.writeNoneASM(var1, var2, var3, var4, var5);
        } else {
            var9.write(91);
            String var6 = "age";
            Integer var11;
            if ((var11 = var10.getAge()) == null) {
                var9.writeNull();
            } else if (var11.getClass() == Integer.class) {
                if (this.age_asm_ser_ == null) {
                    this.age_asm_ser_ = var1.getObjectWriter(Integer.class);
                }

                ObjectSerializer var12 = this.age_asm_ser_;
                if (var12 instanceof JavaBeanSerializer) {
                    ((JavaBeanSerializer)var12).writeAsArrayNonContext(var1, var11, var6, Integer.class, 0);
                } else {
                    var12.write(var1, var11, var6, Integer.class, 0);
                }
            } else {
                var1.writeWithFieldName(var11, var6, this.age_asm_fieldType, 0);
            }

            var9.write(44);
            var6 = "name";
            var9.writeString(var10.getName(), ']');
        }
    }
}
